package cn.thoughtworks.school.programCenter.controllers;

import cn.thoughtworks.school.programCenter.annotations.Auth;
import cn.thoughtworks.school.programCenter.entities.AssignmentQuizMark;
import cn.thoughtworks.school.programCenter.entities.User;
import cn.thoughtworks.school.programCenter.exceptions.BusinessException;
import cn.thoughtworks.school.programCenter.services.AssignmentQuizMarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/api")
public class AssignmentQuizMarkController {
    @Autowired
    private AssignmentQuizMarkService assignmentQuizMarkService;

    @GetMapping("assignments/{assignmentId}/quizzes/{quizId}/students/{studentId}/assignmentQuizMarks")
    public ResponseEntity getAssignmentQuizMark(@PathVariable Long assignmentId,
                                                       @PathVariable Long quizId,
                                                       @PathVariable Long studentId,
                                                       @Auth User current) throws BusinessException {
        AssignmentQuizMark assignmentQuizMark =
                assignmentQuizMarkService.getAssignmentQuizMark(assignmentId, quizId, studentId, current.getId());

        return new ResponseEntity(assignmentQuizMark, HttpStatus.OK);
    }

    @PutMapping("assignments/assignmentQuizMarks")
    public ResponseEntity editAssignmentQuizMark(@RequestBody AssignmentQuizMark assignmentQuizMark) throws BusinessException {
        assignmentQuizMarkService.editAssignmentQuizMark(assignmentQuizMark);

        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @PostMapping("assignments/assignmentQuizMarks")
    public ResponseEntity addAssignmentQuizMark(@Auth User current, @RequestBody AssignmentQuizMark assignmentQuizMark) throws BusinessException {
        Map<String, Object> result = new HashMap<>();
        AssignmentQuizMark assignmentQuizMarkInfo = assignmentQuizMarkService.addAssignmentQuizMark(current.getId(), assignmentQuizMark);
        result.put("id", assignmentQuizMarkInfo.getId());

        return new ResponseEntity(result, HttpStatus.CREATED);
    }
}